#include "textline.h"

TextLine::TextLine(){ }

void TextLine::draw(QPaintDevice* device, qreal scale)
{
    QPainter painter;
    QPen pen;
    QBrush brush;

    pen.setColor(this->strokeColor);
    pen.setWidth(this->strokeWidth);
    pen.setJoinStyle(Qt::MiterJoin);

    brush.setColor(this->fillColor);
    brush.setStyle(Qt::SolidPattern);

    painter.begin(device);

    painter.scale(scale, scale);
    painter.setPen(pen);
    painter.setBrush(brush);

    painter.setFont(this->textFont);

    painter.drawText(this->topLeftPoint, this->text);

    painter.end();
}

QDomElement TextLine::saveToNode(QDomDocument &domDoc)
{
    QDomElement domElement = domDoc.createElement("text");
    QMap<QString,QString> styleMap;

    styleMap.insert("fill",getHexColor(fillColor));
    styleMap.insert("stroke",getHexColor(strokeColor));
    styleMap.insert("fill-opacity",QString::number(fillColor.alphaF()));
    styleMap.insert("stroke-opacity",QString::number(strokeColor.alphaF()));
    styleMap.insert("stroke-width",QString::number(strokeWidth));

    styleMap.insert("font-family",textFont.family());
    styleMap.insert("font-size", QString::number(textFont.pixelSize()));

    domElement.setAttribute("style",constructStyle(styleMap));

    QDomElement domTSpan = domDoc.createElement("tspan");
    domTSpan.setAttribute("x",this->topLeftPoint.x());
    domTSpan.setAttribute("y",this->topLeftPoint.y());

    QDomText domText = domDoc.createTextNode(this->text);

    domElement.appendChild(domTSpan);
    domTSpan.appendChild(domText);

    return domElement;
}

void TextLine::loadFromNode(const QDomElement &node)
{
    // Читаем атрибут style
    QString style = node.attribute("style","");
    QMap<QString, QString> stylemap = parseStyle(style);

    // Цвет заполнения
    if (stylemap.contains("fill"))
    {
        QString fillValue = stylemap.value("fill");
        if(fillValue == "none")        
            fillColor = QColor(0,0,0,0);        
        else        
            fillColor = QColor(fillValue);        
    }

    // Цвет линии
    if (stylemap.contains("stroke"))
    {
        QString strokeValue = stylemap.value("stroke");
        if(strokeValue == "none")        
            strokeColor = QColor(0,0,0,0);        
        else        
            strokeColor = QColor(strokeValue);        
    }

    // Толщина линии
    if (stylemap.contains("stroke-width"))
    {
        QString strokeWidthValue = stylemap.value("stroke-width");
        strokeWidth = strokeWidthValue.toInt();
    }

    // Прозрачность заполнения
    if (stylemap.contains("fill-opacity"))    
        fillColor.setAlphaF(stylemap.value("fill-opacity").toFloat());

    // Прозрачность линии
    if (stylemap.contains("stroke-opacity"))    
        strokeColor.setAlphaF(stylemap.value("stroke-opacity").toFloat());    

    if (stylemap.contains("font-family"))
        this->textFont.setFamily(stylemap.value("font-family"));

    if (stylemap.contains("font-size"))
    {
        QString fontSizeValue = stylemap.value("font-size");
        this->textFont.setPixelSize(fontSizeValue.toInt());
    }

    QDomElement tspan = node.firstChildElement("tspan");
    text = tspan.text();
    topLeftPoint.setX(tspan.attribute("x","").toInt());
    topLeftPoint.setY(tspan.attribute("y","").toInt());
}
